/*
 * Author: yezi
 * E-mail: okye328@gmail.com
 * Create time: Dec 31, 2012 11:18:31 AM
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 *
 */
package com.kanbox.linux.fs;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONObject;

public class INode {

	private static Logger logger = LoggerFactory.getLogger(INode.class);

	private static final DateFormat dateFormat = new SimpleDateFormat(
			"yyyy-MM-dd hh:mm:ss");
	private static final long EIGHT_HOUR = 8 * 3600 * 1000;

	private int fileSize;
	private String fullPath;
	private boolean isFolder;
	private boolean isShared;
	private long modificationDate;

	public int getFileSize() {
		return fileSize;
	}

	public void setFileSize(int fileSize) {
		this.fileSize = fileSize;
	}

	public String getFullPath() {
		return fullPath;
	}

	public void setFullPath(String fullPath) {
		this.fullPath = fullPath;
	}

	public boolean isFolder() {
		return isFolder;
	}

	public void setFolder(boolean isFolder) {
		this.isFolder = isFolder;
	}

	public boolean isShared() {
		return isShared;
	}

	public void setShared(boolean isShared) {
		this.isShared = isShared;
	}

	public long getModificationDate() {
		return modificationDate;
	}

	public void setModificationDate(long modificationDate) {
		this.modificationDate = modificationDate;
	}

	public void setModificationDate(String modificationDate) {
		modificationDate = modificationDate.replace("T", " ").replace("+00:00",
				"");
		Date date = null;
		try {
			date = dateFormat.parse(modificationDate);
			this.modificationDate = date.getTime() + EIGHT_HOUR;
		} catch (ParseException e) {
			logger.error("Parsing date error", e);
			this.modificationDate = 0;
		}
	}

	public static INode fromJSON(JSONObject jsonObject) {
		INode iNode = new INode();
		iNode.setFileSize(jsonObject.getIntValue("fileSize"));
		iNode.setFullPath(jsonObject.getString("fullPath"));
		iNode.setFolder(jsonObject.getBooleanValue("isFolder"));
		iNode.setShared(jsonObject.getBooleanValue("isShared"));
		iNode.setModificationDate(jsonObject.getString("modificationDate"));
		return iNode;
	}

}
